/*
 * $Id$
 *
 * SARL is an general-purpose agent programming language.
 * More details on http://www.sarl.io
 *
 * Copyright (C) 2014-2024 SARL.io, the Original Authors and Main Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *------- FORKED SOURCE CODE:
 *
 * THIS CODE IS FORKED FROM JDK.JAVADOC INTERNAL PACKAGE AND ADAPTED TO THE SARL PURPOSE.
 * THE FORK WAS NECESSARY BECAUSE IT IS IMPOSSIBLE TO SUBCLASS THE TYPES FOR THE.
 * STANDARD HTML DOCLET THAT IS PROVIDED BY JDK.JAVADOC MODULE.
 *
 * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package io.sarl.docs.doclet2.framework;

import javax.lang.model.element.Element;

import com.google.inject.Provider;
import com.sun.source.doctree.BlockTagTree;
import com.sun.source.doctree.InlineTagTree;

import io.sarl.docs.doclet2.html.taglets.block.ExcludeFromApidocTaglet;
import io.sarl.docs.doclet2.html.taglets.block.HiddenTaglet;
import io.sarl.lang.core.annotation.SyntheticMember;
import io.sarl.lang.core.util.SarlUtils;

/** Check if an element should be ignored into the API doc.
 * 
 * <p>The ignored elements are:<ul>
 * <li>elements with an "hidden" name (usually with the {@code $} character)</li>
 * <li>elements tagged with {@code @SyntheticMember} that are usually generated by the SARL compiler</li>
 * <li>elements manually tagged with {@code @ExcludeFromApiDoc}</li>
 * </ul>
 *
 * @author $Author: sgalland$
 * @version $FullVersion$
 * @mavengroupid $GroupId$
 * @mavenartifactid $ArtifactId$
 * @since 0.13
 */
public class DefaultApidocExcluder implements ApidocExcluder {

	private Provider<SarlDocletEnvironment> googleEnvironmentProvider;

	private SarlDocletEnvironment environment;

	/** Constructor.
	 *
	 * @param environmentProvider the provider of the generation environment.
	 */
	public DefaultApidocExcluder(Provider<SarlDocletEnvironment> environmentProvider) {
		this.googleEnvironmentProvider = environmentProvider;
	}

	/** Replies the doclet generation environment.
	 *
	 * @return the environment.
	 */
	public SarlDocletEnvironment getDocletEnvironment() {
		if (this.environment == null) {
			this.environment = this.googleEnvironmentProvider.get();
		}
		return this.environment;
	}

	/** Replies if the given name is for a tag that causes the element to be hidden.
	 *
	 * @param tagName the tag name.
	 * @return {@code true} if the element must be hidden.
	 */
	@SuppressWarnings("static-method")
	protected boolean isExcluded(String tagName) {
		return ExcludeFromApidocTaglet.TAG_NAME.equalsIgnoreCase(tagName) || HiddenTaglet.TAG_NAME.equalsIgnoreCase(tagName);
	}
	
	@Override
	public boolean isExcluded(Element element) {
		if (SarlUtils.isHiddenMember(element.getSimpleName().toString())) {
			return true;
		}
		if (element.getAnnotation(SyntheticMember.class) != null) {
			return true;
		}
		final var env = getDocletEnvironment();
		final var tree = env.getDocTrees();
		final var comment = tree.getDocCommentTree(element);
		if (comment != null) {
			final var nodes = comment.getBlockTags();
			if (nodes != null) {
				for (final var node : nodes) {
					if (node instanceof InlineTagTree tag) {
						if (isExcluded(tag.getTagName())) {
							return true;
						}
					} else if (node instanceof BlockTagTree tag) {
						if (isExcluded(tag.getTagName())) {
							return true;
						}
					}
				}
			}
		}
		// nothing above found a reason to exclude
		return false;
	}

	@Override
	public boolean isTranslatableToTag(Element element) {
		if (element.getAnnotation(SyntheticMember.class) != null) {
			return true;
		}
		return false;
	}

}
